package com.helen.ui.newui;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helen.adapter.QuestionListAdapter;
import com.helen.entity.QuestionEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.MyLog;
import com.helen.utils.PhoneUtils;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yang on 2019/1/31
 */
public class CustomerActivity extends BaseActivity {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    private List<QuestionEntity> list = new ArrayList();
    private QuestionListAdapter questionListAdapter;
    private View errorView;
    private int page = 1;
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_coupons_list;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("客服解答");
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvList.getParent(), false);
        questionListAdapter = new QuestionListAdapter(list);
        questionListAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        swipeRefresh.setRefreshing(true);
        swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.appThemeColor));
        rvList.setLayoutManager(new LinearLayoutManager(CustomerActivity.this));
        rvList.setAdapter(questionListAdapter);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        questionListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, rvList);
        refresh();
    }

    @Override
    protected void updateViews() {

    }

    private void refresh() {
        page = 1;
        getData();
    }


    private void loadMore() {
        page++;
        getData();
    }
    private void getData() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("page", page + "");
        params.put("size", "10");
        EasyHttp.post(Constants.API_JSONAPI_QUESTION_LIST)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "获取问题失败==" + e.toString());
                        MToast.makeTextShort(CustomerActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String s) {
                        MyLog.d("yang", "s===" + s);
                        String data = PhoneUtils.checkResponseFlag2(CustomerActivity.this, s, TAG);
                        if (data != null) {
                            list = new Gson().fromJson(data, new TypeToken<List<QuestionEntity>>() {
                            }.getType());
                            if (page == 1) {
                                questionListAdapter.getData().clear();
                                questionListAdapter.setNewData(list);
                            } else {
                                questionListAdapter.addData(list);
                            }
                            if (list.size() < 10) {
                                questionListAdapter.loadMoreEnd(false);
                                questionListAdapter.setEmptyView(errorView);
                            } else {
                                questionListAdapter.loadMoreComplete();
                            }
                        }

                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                        swipeRefresh.setRefreshing(false);
                    }
                });
    }
}
